package com.tom.demo.controller.system;
import com.tom.demo.common.annotation.OperaLog;
import com.tom.demo.common.enumeration.OperaTypeEnum;
import com.tom.demo.common.response.Result;
import com.tom.demo.dto.AssignUserRoleDto;
import com.tom.demo.dto.UserEditDto;
import com.tom.demo.dto.UserDto;
import com.tom.demo.dto.UserPasswordDto;
import com.tom.demo.service.system.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping(path = {"/user"})
@Api(tags = {"用户管理"})
public class UserController {

    @Resource
    private UserService userService;

    @PutMapping(path = {"/updatePassword"})
    @ApiOperation(value = "修改用户密码")
    public Result updatePassword(@RequestBody @Validated UserPasswordDto userPasswordDto){
        return userService.updatePassword(userPasswordDto);
    }


    @PostMapping(path = {"/pageList"})
    @ApiOperation(value = "获取用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", defaultValue = "1", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页查询多少条", defaultValue = "5", dataType = "int", paramType = "query")
    })
    @PreAuthorize(value = "hasAuthority('system:user:list')")
    public Result pageList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                           @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                           @RequestBody UserDto userDto){
        return userService.pageList(page,pageSize,userDto);
    }


    @PostMapping(path = {"/insertUser"})
    @ApiOperation(value = "添加用户")
    @PreAuthorize(value = "hasAuthority('system:user:add')")
    @OperaLog(operaModel = "用户管理",operaType = OperaTypeEnum.ADD,operaDesc = "添加用户")
    public Result insertUser(UserEditDto userEditDto){
        return userService.insertUser(userEditDto);
    }

    @PutMapping(path = {"/updateUserStatus"})
    @ApiOperation(value = "修改用户状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "String"),
            @ApiImplicitParam(name = "status", value = "状态true是禁用 false:正常", dataType = "Boolean")
    })
    @OperaLog(operaModel = "用户管理",operaType = OperaTypeEnum.UPDATE,operaDesc = "修改用户状态")
    public Result updateUserStatus(@RequestParam(value = "userId")String userId,
                                   @RequestParam(value = "status")Boolean status){
        return userService.updateUserStatus(userId,status);
    }

    @GetMapping(path = {"/findByUserId/{userId}"})
    @ApiOperation(value = "根据用户id获取用户信息回显")
    @ApiImplicitParam(name = "roleId", value = "用户ID", required = true, dataType = "String", paramType = "path")
    public Result findByUserId(@PathVariable(value = "userId")String userId){
        return userService.findByUserId(userId);
    }

    @DeleteMapping(path = {"/deleteByUserId/{userId}"})
    @ApiOperation(value = "根据用户id删除用户信息")
    @ApiImplicitParam(name = "roleId", value = "用户ID", required = true, dataType = "String", paramType = "path")
    @OperaLog(operaModel = "用户管理",operaType = OperaTypeEnum.DELETE,operaDesc = "根据用户id删除用户信息")
    public Result deleteByUserId(@PathVariable(value = "userId")String userId){
        return userService.deleteByUserId(userId);
    }

    @PutMapping(path = {"/updateUser"})
    @ApiOperation(value = "修改用户信息")
    @OperaLog(operaModel = "用户管理",operaType = OperaTypeEnum.UPDATE,operaDesc = "修改用户状态")
    public Result updateUser(UserEditDto userEditDto) throws Exception {
        return userService.updateUser(userEditDto);
    }

    @GetMapping(path = {"/toAssign/{userId}"})
    @ApiOperation(value = "根据用户id获取当前所属的角色")
    public Result toAssign(@PathVariable(value = "userId")String userId){
        return userService.toAssign(userId);
    }

    @PostMapping(path = {"/doAssign"})
    @ApiOperation(value = "为用户分配角色")
    @OperaLog(operaModel = "用户管理",operaType = OperaTypeEnum.UPDATE,operaDesc = "为用户分配角色")
    public Result doAssign(@RequestBody AssignUserRoleDto assignUserRoleDto){
        return userService.doAssign(assignUserRoleDto);
    }

    @GetMapping(path = {"/getUserOptions"})
    @ApiOperation(value = "获取用户信息选项")
    public Result getUserOptions(){
        return userService.getUserOptions();
    }

}
